<template>
  <div class="wait-config">
    <div class="grid grid-cols-1 gap-4">
      <div class="grid grid-cols-2 gap-3">
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">等待类型</label>
          <el-select 
            v-model="localStep.waitType" 
            size="small"
            @change="updateStep"
          >
            <el-option label="等待时间" value="timeout" />
            <el-option label="等待元素可见" value="visible" />
            <el-option label="等待元素隐藏" value="hidden" />
            <el-option label="等待元素存在" value="attached" />
            <el-option label="等待元素移除" value="detached" />
            <el-option label="等待页面加载" value="load" />
            <el-option label="等待网络空闲" value="networkidle" />
            <el-option label="等待函数返回" value="function" />
          </el-select>
        </div>
        
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">等待时间 (ms)</label>
          <el-input-number
            v-model="localStep.waitTime"
            :min="100"
            :step="100"
            size="small"
            class="w-full"
            @change="updateStep"
          />
        </div>
      </div>
      
      <div v-if="localStep.waitType === 'function'">
        <label class="block text-sm font-medium text-gray-700 mb-1">等待函数</label>
        <el-input
          v-model="localStep.waitFunction"
          type="textarea"
          :rows="3"
          placeholder="() => document.readyState === 'complete'"
          size="small"
          @change="updateStep"
        />
        <div class="text-sm text-gray-600 mt-1">
          <el-icon class="mr-1"><i-ep-info-filled /></el-icon>
          输入一个返回布尔值的JavaScript函数
        </div>
      </div>
      
      <div v-if="needsSelector" class="grid grid-cols-1 gap-3">
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">目标元素选择器</label>
          <el-input
            v-model="localStep.waitSelector"
            placeholder="CSS选择器或XPath"
            size="small"
            @change="updateStep"
          >
            <template #prepend>
              <el-icon><i-ep-aim /></el-icon>
            </template>
          </el-input>
        </div>
      </div>
      
      <div class="border-t border-gray-200 pt-4">
        <h4 class="text-sm font-medium text-gray-700 mb-3">等待选项</h4>
        <div class="grid grid-cols-2 gap-3">
          <el-checkbox 
            v-model="localStep.continueOnTimeout"
            @change="updateStep"
          >
            超时后继续执行
          </el-checkbox>
          <el-checkbox 
            v-model="localStep.throwOnTimeout"
            @change="updateStep"
          >
            超时时抛出错误
          </el-checkbox>
          <el-checkbox 
            v-model="localStep.logWaitProgress"
            @change="updateStep"
          >
            记录等待进度
          </el-checkbox>
          <el-checkbox 
            v-model="localStep.screenshotOnTimeout"
            @change="updateStep"
          >
            超时时截图
          </el-checkbox>
        </div>
      </div>
      
      <div class="grid grid-cols-2 gap-3">
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">超时时间 (ms)</label>
          <el-input-number
            v-model="localStep.timeout"
            :min="1000"
            :step="1000"
            size="small"
            class="w-full"
            @change="updateStep"
          />
        </div>
        
        <div>
          <label class="block text-sm font-medium text-gray-700 mb-1">检查间隔 (ms)</label>
          <el-input-number
            v-model="localStep.polling"
            :min="100"
            :step="100"
            size="small"
            class="w-full"
            @change="updateStep"
          />
        </div>
      </div>
      
      <div v-if="localStep.waitType === 'networkidle'">
        <label class="block text-sm font-medium text-gray-700 mb-1">网络空闲阈值</label>
        <el-select 
          v-model="localStep.networkIdleTimeout" 
          size="small"
          @change="updateStep"
        >
          <el-option label="500ms 无网络请求" value="500" />
          <el-option label="1秒 无网络请求" value="1000" />
          <el-option label="2秒 无网络请求" value="2000" />
        </el-select>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { InfoFilled as iEpInfoFilled, Aim as iEpAim } from '@element-plus/icons-vue'

const props = defineProps({
  step: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['update'])

const localStep = ref({
  waitType: 'timeout',
  waitTime: 1000,
  waitFunction: '',
  waitSelector: '',
  continueOnTimeout: false,
  throwOnTimeout: true,
  logWaitProgress: false,
  screenshotOnTimeout: false,
  timeout: 30000,
  polling: 1000,
  networkIdleTimeout: '500',
  ...props.step
})

watch(() => props.step, (newStep) => {
  localStep.value = {
    waitType: 'timeout',
    waitTime: 1000,
    waitFunction: '',
    waitSelector: '',
    continueOnTimeout: false,
    throwOnTimeout: true,
    logWaitProgress: false,
    screenshotOnTimeout: false,
    timeout: 30000,
    polling: 1000,
    networkIdleTimeout: '500',
    ...newStep
  }
}, { deep: true })

// 计算是否需要选择器输入
const needsSelector = computed(() => {
  const selectorRequiredTypes = ['visible', 'hidden', 'attached', 'detached']
  return selectorRequiredTypes.includes(localStep.value.waitType)
})

const updateStep = () => {
  emit('update', localStep.value)
}
</script>